/**
 * easyui控件依赖关系
 */
export let depents = {
    pagination: { dependencies: ['linkbutton'] },
    datagrid: { dependencies: ['panel', 'resizable', 'linkbutton', 'pagination'] },
    treegrid: { dependencies: ['datagrid'] },
    propertygrid: { dependencies: ['datagrid'] },
    datalist: { dependencies: ['datagrid'] },
    window: { dependencies: ['resizable', 'draggable', 'panel'] },
    dialog: { dependencies: ['linkbutton', 'window'] },
    messager: { dependencies: ['linkbutton', 'window', 'progressbar'] },
    layout: { dependencies: ['resizable', 'panel'] },
    tabs: { dependencies: ['panel', 'linkbutton'] },
    menubutton: { dependencies: ['linkbutton', 'menu'] },
    splitbutton: { dependencies: ['menubutton'] },
    accordion: { dependencies: ['panel'] },
    textbox: { dependencies: ['validatebox', 'linkbutton'] },
    filebox: { dependencies: ['textbox'] },
    combo: { dependencies: ['panel', 'textbox'] },
    combobox: { dependencies: ['combo'] },
    combotree: { dependencies: ['combo', 'tree'] },
    combogrid: { dependencies: ['combo', 'datagrid'] },
    combotreegrid: { dependencies: ['combo', 'treegrid'] },
    validatebox: { dependencies: ['tooltip'] },
    numberbox: { dependencies: ['textbox'] },
    searchbox: { dependencies: ['menubutton', 'textbox'] },
    spinner: { dependencies: ['textbox'] },
    numberspinner: { dependencies: ['spinner', 'numberbox'] },
    timespinner: { dependencies: ['spinner'] },
    tree: { dependencies: ['draggable', 'droppable'] },
    datebox: { dependencies: ['calendar', 'combo'] },
    datetimebox: { dependencies: ['datebox', 'timespinner'] },
    slider: { dependencies: ['draggable'] }
}
/**
 * 获取依赖项
 * @param name 
 */
export function getDepend(name:string){
    let deps = depents[name];
    let depret = [];
    if(deps && deps.dependencies){
        deps.dependencies.forEach(d => {
            if(depents[d]){
                depret = depret.concat(getDepend(d));
            }
            depret.push(d);
        });
    }
    return depret;
}

/**
 * 根据组件名、方法名，获取此方法归属组件（因为方法有可能归属于依赖组件）
 * @param componentName 组件名
 * @param methodName 方法名
 * @param jquery jquery对象
 */
export function getDependNameOfComponent(componentName:string, methodName:string, argdeps, jquery:JQueryStatic):string{
    let methods = jquery.fn[componentName].methods;
    let result = null;
    Object.getOwnPropertyNames(methods).forEach(m => {
        if(m == methodName ){
            result = componentName;
            return false;
        }
    });
    if(!result){
        let deps = depents[componentName] ? depents[componentName].dependencies : null; 
        if(deps){
            deps.forEach(d => {
                if(!result){
                    let tmp = getDependNameOfComponent(d, methodName, argdeps, jquery); 

                    if( tmp && argdeps){ //如果查询到的组件名在依赖列表内，可使用
                        let depkeys = Object.getOwnPropertyNames(argdeps).join(',');
                        if(depkeys.indexOf(tmp) >=0){
                            result = tmp;
                        }
                    }
                }
            }); 
        }
    }
    return result;
}